package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/7/10 11:36.
 * 实名认证成功页面
 */
public class RealNamePassActivity extends BaseActivity {
    public static final String REAL_NAME = "realName";
    public static final String IDENTIFY = "identify";
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_identify)
    TextView tvIdentify;

    @Override
    protected void onResume() {
        super.onResume();
        GoogleAnalyticsUtil.setScreenName(Google.REAL_NAME_RESULT);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        mCurrentPageId= Self.Page.app_identify_verify_pass_page;

        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        String realName = intent.getStringExtra(REAL_NAME);
        String identify = intent.getStringExtra(IDENTIFY);
        if (TextUtils.isEmpty(realName) || TextUtils.isEmpty(identify)) {
            loadData();
        } else {
            tvName.setText(handleRealName(realName));
            tvIdentify.setText(handlerIdentify(identify));
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_real_name_pass;
    }


    /**
     * 对姓名进行*处理
     */
    private String handleRealName(String realName) {
        StringBuilder nameSb = new StringBuilder();
        int nameLength = realName.length();
        for (int i = 0; i < nameLength; i++) {
            if (i == nameLength - 1) {
                nameSb.append(realName.charAt(i));
            } else {
                nameSb.append("*");
            }
        }
        return nameSb.toString();
    }

    /**
     * 对身份证进行*处理
     */
    private String handlerIdentify(String idCardNo) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, length = idCardNo.length(); i < length; i++) {
            if (i == length - 8 || i == length - 7 || i == length - 6 || i == length - 5) {
                sb.append("*");
            } else {
                sb.append(idCardNo.charAt(i));
            }
        }
        return sb.toString();
    }

    private void loadData() {
        showProgressDialog(false);
        Observable<String> observable= Api.getMemberInfo(mApplication.getUser().getID());
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                dismissProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                JSONObject json = JSON.parseObject(s);
                if (json != null) {
                    if (json.getIntValue("code") == 1) {
                        JSONObject jsonObject = json.getJSONObject("data");
                        if (jsonObject != null) {
                            JSONObject object = jsonObject.getJSONObject("MemberInfo");
                            if (object != null) {
                                String name = object.getString("TrueName");
                                String idNumber = object.getString("IDCardNo");
                                tvName.setText(handleRealName(name));
                                tvIdentify.setText(handlerIdentify(idNumber));
                            }
                        }
                    } else {
                        ToastUtil.showToast(json.getString("msg"));
                    }
                }
            }
        });
    }
}
